package com.itheima.jobs;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.RedisConstant;
import com.itheima.service.ApiCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class DateBaseSyncJob {
    @Autowired
    private JedisPool jedisPool;

    @Reference
    private ApiCountService apiCountService;

    @Scheduled(cron = "0/50 * * * * ? ")
    public void dateBaseSync() {
        try {
            List<Map> AList = apiCountService.findApiCount4Redis();

            for (Map map : AList) {
                Long time = 9999999999999L - Long.parseLong((String)map.get("time"));
                Object count = map.get("count");
                Object apiName = map.get("apiName");
                Double score = Double.parseDouble(count + "." + time);
                jedisPool.getResource().zadd(RedisConstant.API_COUNT, score, apiName.toString());
            }
            System.out.println("数据库同步成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
